﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietPublisher.BSL.BK;
using VietPublisher.Repository.Repositories.Concrete;
using VietPublisher.BaseProject.Models;



namespace VietPublisher.Repositories.Interface
{
    public class BookRepository : BaseRepository, IBookRepository
    {

        public BookRepository() { }


        public List<T_BK_Book> BookGetList(string BookTitle, string BookAuthor)
        {
            throw new NotImplementedException();
        }

        public bool CreateBook(T_BK_Book book)
        {
            //step 1 get 4 cái khoa ngoai 
            var Picture = ContextBookStore.T_SC_FileGroup.FirstOrDefault(m => m.GroupId == (book.PictureID ?? 0));
            var Publisher = ContextBookStore.T_BK_Publisher.FirstOrDefault(m => m.PublisherID == (book.PublisherID ?? 0));
            var AuthorGroup = ContextBookStore.T_BK_AuthorGroup.FirstOrDefault(m => m.AuthorGroupID == (book.AuthorGroupID ?? 0));
            var Category = ContextBookStore.T_BK_Category.FirstOrDefault(m => m.CategoryID == (book.CategoryID ?? 0));
            //step 2 insert Book
            book.T_BK_AuthorGroup = AuthorGroup;
            book.T_BK_Category = Category;
            book.T_BK_Publisher = Publisher;
            book.T_SC_FileGroup = Picture;

            ContextBookStore.T_BK_Book.AddObject(book);
            var result = ContextBookStore.SaveChanges();
            return result == 1 ? true : false;
        }

        public int BookUpdate(T_BK_Book book)
        {
            ////step 1 get 4 cái khoa ngoai 
            var Picture = ContextBookStore.T_SC_FileGroup.FirstOrDefault(m => m.GroupId == (book.PictureID ?? 0));
            var Publisher = ContextBookStore.T_BK_Publisher.FirstOrDefault(m => m.PublisherID == (book.PublisherID ?? 0));
            var AuthorGroup = ContextBookStore.T_BK_AuthorGroup.FirstOrDefault(m => m.AuthorGroupID == (book.AuthorGroupID ?? 0));
            var Category = ContextBookStore.T_BK_Category.FirstOrDefault(m => m.CategoryID == (book.CategoryID ?? 0));
            ////step 2 insert Book
            //book.T_BK_AuthorGroup = AuthorGroup;
            //book.T_BK_Category = Category;
            //book.T_BK_Publisher = Publisher;
            //book.T_SC_FileGroup = Picture;
            var BookUpd= ContextBookStore.T_BK_Book.FirstOrDefault(m=>m.BookID== book.BookID);
            if(BookUpd!= null){
                BookUpd.AuthorGroupID= book.AuthorGroupID;
                BookUpd.BookCode= book.BookCode;
                BookUpd.BookTitle= book.BookTitle;
                BookUpd.CategoryID= book.CategoryID;
                BookUpd.Description=book.Description;
                BookUpd.Language=book.Language;
                BookUpd.PageNumber=book.PageNumber;
                BookUpd.PictureID=book.PictureID;
                BookUpd.PriceCurrent=book.PriceCurrent;
                BookUpd.PublisherDate=book.PublisherDate;
                BookUpd.PublisherID=book.PublisherID;
                BookUpd.Quantity=book.Quantity;
                BookUpd.Size=book.Size;
                BookUpd.Translator=book.Translator;
                BookUpd.Weight=book.Weight;
                BookUpd.T_BK_AuthorGroup = AuthorGroup;
                BookUpd.T_BK_Category = Category;
                BookUpd.T_BK_Publisher = Publisher;
                BookUpd.T_SC_FileGroup = Picture;
            }

            ContextBookStore.T_BK_Book.ApplyCurrentValues(BookUpd);
            var result = ContextBookStore.SaveChanges();
            return result ;
        }

        public int BookDelete(int BookID)
        {
            throw new NotImplementedException();
        }

        public List<ListBook> GetBooks()
        {
            #region join multi table by entity
            //List<ListBook> books = ContextBookStore.T_BK_Book
            //    .Join(ContextBookStore.T_BK_Publisher,bk=>bk.PublisherID,pl=>pl.PublisherID,(bk,pl)=>bk)
            //    .Join(ContextBookStore.T_BK_AuthorGroup,bk=>bk.AuthorGroupID,au=>au.AuthorGroupID,(bk,au)=>bk)                
            //    .Select(bk => new ListBook()
            //{
            //    PriceCurrent = bk.PriceCurrent,
            //    PictureID = bk.PictureID,
            //    BookTitle = bk.BookTitle

            //}).ToList();
            #endregion

            #region join muli table by LINQ
            var books = (
                          from bk in ContextBookStore.T_BK_Book
                          join pl in ContextBookStore.T_BK_Publisher
                               on bk.PublisherID equals pl.PublisherID
                          join au in ContextBookStore.T_BK_AuthorGroup
                          on bk.AuthorGroupID equals au.AuthorGroupID
                          select new ListBook()
                          {
                              BookTitle = bk.BookTitle,
                              BookCode = bk.BookCode,
                              PictureID = bk.PictureID,
                              PriceCurrent = bk.PriceCurrent,
                              BookID = bk.BookID
                          }
                           ).ToList();
            #endregion
            return books;
        }



        public List<ListBook> GetTopBooks(int countSelect)
        {
            #region join multi table by entity
            //List<ListBook> books = ContextBookStore.T_BK_Book
            //    .Join(ContextBookStore.T_BK_Publisher,bk=>bk.PublisherID,pl=>pl.PublisherID,(bk,pl)=>bk)
            //    .Join(ContextBookStore.T_BK_AuthorGroup,bk=>bk.AuthorGroupID,au=>au.AuthorGroupID,(bk,au)=>bk)                
            //    .Select(bk => new ListBook()
            //{
            //    PriceCurrent = bk.PriceCurrent,
            //    PictureID = bk.PictureID,
            //    BookTitle = bk.BookTitle

            //}).ToList();
            #endregion

            #region join muli table by LINQ
            var books = (
                          from bk in ContextBookStore.T_BK_Book
                          join pl in ContextBookStore.T_BK_Publisher
                               on bk.PublisherID equals pl.PublisherID
                          join au in ContextBookStore.T_BK_AuthorGroup
                          on bk.AuthorGroupID equals au.AuthorGroupID
                          select new ListBook()
                          {
                              BookTitle = bk.BookTitle,
                              BookCode = bk.BookCode,
                              PictureID = bk.PictureID,
                              PriceCurrent = bk.PriceCurrent,
                              BookID = bk.BookID
                          }
                           ).Take(countSelect).OrderByDescending(p=>p.BookID).ToList();
            #endregion
            return books;
        }

        public ViewBook GetBook(int bookID)
        {
            var book = (
                         from bk in ContextBookStore.T_BK_Book
                         join pl in ContextBookStore.T_BK_Publisher
                              on bk.PublisherID equals pl.PublisherID
                         join auG in ContextBookStore.T_BK_AuthorGroup
                         on bk.AuthorGroupID equals auG.AuthorGroupID
                         where bk.BookID == bookID
                         select new ViewBook()
                         {
                             Translator = bk.Translator,
                             BookId = bk.BookID,
                             PublisherName = pl.PublisherName,
                             pictureID = bk.PictureID,
                             AuthorName = auG.AuthorIds, //id of authors in GroupAuthor -- this will change to name of author
                             PublisherDate = bk.PublisherDate,
                             PriceCurrent = bk.PriceCurrent,
                             PageNumber = bk.PageNumber,
                             BookTitle = bk.BookTitle,
                             Size = bk.Size,
                             Weight = bk.Weight,
                             BookCode = bk.BookCode
                         }
                          ).FirstOrDefault();

            if (book != null)
            {
                var authorIds = new List<int>();
                var tmp = book.AuthorName.Split(',');
                foreach (var i in tmp)
                {
                    if (string.IsNullOrEmpty(i))
                    {
                        continue;
                    }
                    authorIds.Add(int.Parse(i));
                }
                var Authors = ContextBookStore.T_BK_Author.Where(m => authorIds.Contains(m.AuthorID)).ToList();
                string authorName = "";
                foreach (var aut in Authors)
                {
                    authorName = "," + aut.AuthorName;
                }
                book.AuthorName = authorName;// gan authorName tro lai vao book
            }

            return book;
        }


        public List<ListBook> GetBooksByCategoryID(int categoryId)
        {
            #region join muli table by LINQ
            var books = (
                          from bk in ContextBookStore.T_BK_Book
                          join pl in ContextBookStore.T_BK_Publisher
                               on bk.PublisherID equals pl.PublisherID
                          join au in ContextBookStore.T_BK_AuthorGroup
                          on bk.AuthorGroupID equals au.AuthorGroupID
                          where bk.CategoryID == categoryId
                          select new ListBook()
                          {
                              BookTitle = bk.BookTitle,
                              BookCode = bk.BookCode,
                              PictureID = bk.PictureID,
                              PriceCurrent = bk.PriceCurrent,
                              BookID = bk.BookID
                          }
                           ).ToList();
            #endregion
            return books;
        }


        public ViewBookDescription ViewBookDescription(int bookId)
        {
            var description = (from bk in ContextBookStore.T_BK_Book
                               where bk.BookID == bookId
                               select new ViewBookDescription()
                               {
                                   Description = bk.Description
                               })
                              .FirstOrDefault();
            return description;
        }


        public int GetQuantityByBookId(int bookId)
        {
            int? quantity = 0;
            quantity = (from bk in ContextBookStore.T_BK_Book
                        where bk.BookID == bookId
                        select bk.Quantity)
                .FirstOrDefault();
            return quantity.HasValue ? quantity.Value : 0;
        }


        public T_BK_Book GetBookByBookID(int bookID)
        {
            var book = (from bk in ContextBookStore.T_BK_Book where bk.BookID == bookID select bk).FirstOrDefault();
            return book;
        }
    }
}
